package cn.task.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.base.handler.StringUtil;
import cn.task.po.Task;
import cn.task.po.TaskVo;
import cn.task.po.Taskmanger;
import cn.task.po.TaskmangerWithBLOBs;
import cn.task.service.TaskMangerService;
import cn.task.service.TaskService;

@Controller
@RequestMapping("/task")
public class ReleaseController {
	@Autowired
	private TaskService taskService;
	@Autowired
	private TaskMangerService taskMangerService;
	@RequestMapping(value = "/releaseTask", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView releaseTask(HttpServletRequest req, HttpServletResponse resp, MultipartFile task_picture,HttpSession session)
			throws Exception {
		Task task = new Task();
		String taskId=UUID.randomUUID().toString();
		String title = req.getParameter("title");
		String type = req.getParameter("type");
		String commission = req.getParameter("commission");
		String number = req.getParameter("number");
		String reservation = req.getParameter("reservation");
		String beginDate = reservation.substring(0, 10);
		String endDate = reservation.substring(12, 23);
		String details = req.getParameter("details");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (StringUtil.isNotEmpty(beginDate) && StringUtil.isNotEmpty(endDate)) {
			Date begin = sdf.parse(beginDate);
			Date end = sdf.parse(endDate);
			task.setBegindate(begin);
			task.setEnddate(end);
		}
		String userAccount=(String) session.getAttribute("useraccount");
		task.setReleaserid("1");
		task.setReleasername(userAccount);
		task.setTaskname(title);
		task.setTasktype(type);
		task.setCity("北京");
		if (details != null && details.length() > 0) {
			task.setDetails(details);
		}
		task.setState("1");
		task.setIseffective("1");
		task.setReleasedate(new Date());
		task.setTaskid(taskId);
		// 原始名称
		String originalFilename = task_picture.getOriginalFilename();
		// 上传图片
		if (task_picture != null && originalFilename != null && originalFilename.length() > 0) {
			// 存储图片的物理路径
			String pic_path = "d:\\upload\\";
			// 新的图片名称
			String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
			// 新图片
			File newFile = new File(pic_path + newFileName);
			// 将内存中的数据写入磁盘
			task_picture.transferTo(newFile);
			// 将新图片名称写到taskVo中
			task.setPicture(newFileName);
		}
		Taskmanger twb=new Taskmanger();
		twb.setUseraccount(userAccount);
		if (StringUtil.isNotEmpty(title) && StringUtil.isNotEmpty(type) && StringUtil.isNotEmpty(title)
				&& StringUtil.isNotEmpty(commission) && StringUtil.isNotEmpty(number)) {
			task.setCommission(Float.parseFloat(commission));
			task.setJoinnumber(number);
			taskService.insertTask(task);
		}
		Taskmanger taskmanger=taskMangerService.selectByAccount(userAccount);
		if(taskmanger!=null)
		{
			String noreceive=taskmanger.getNoreceive();
			StringBuffer sb=new StringBuffer();
			if(noreceive!=null)
			{
				sb.append(noreceive);
			}
			sb.append(taskId+"#");
			twb.setNoreceive(sb.toString());
			taskMangerService.updateRecord(twb);
		}
		else
		{
			twb.setNoreceive(taskId+"#");
			taskMangerService.insertRecord(twb);
		}
		ModelAndView mav=new ModelAndView();
		mav.setViewName("userManager/success");
		return mav;
	}

	@RequestMapping("/my_release")
	public @ResponseBody TaskVo test(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		TaskVo taskVo = new TaskVo();
		Task task = taskService.selectTaskById("2");
		Date begindate = task.getBegindate();
		Date enddate = task.getEnddate();
		Date releasedate = task.getReleasedate();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		String startDate = sdf.format(begindate);
		String overDate = sdf.format(enddate);
		String publishDate = sdf.format(releasedate);
		BeanUtils.copyProperties(task, taskVo);
		taskVo.setStartDate(startDate);
		taskVo.setOverDate(overDate);
		taskVo.setStartDate(startDate);
		taskVo.setPublishDate(publishDate);
		return taskVo;
	}
}
